﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 海漫思考核.BLL;
using 海漫思考核.DAL;
using System.IO;

namespace 海漫思考核
{
    public partial class EditNove : System.Web.UI.Page
    {
        //初始化页面索引
        private static int index = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)//第一次加载
            {
                if (Session["User"] != null)
                {
                    var types = NoveBLL.SelectAllNoveType();//获取所有的小说类型
                    foreach (NoveType item in types)//给前端小说类型下拉框赋值
                    {
                        ddl_type.Items.Add(item.Tname);
                        ddl_type.Items[item.Tid].Value = item.Tname;
                    }
                    ShowData();//显示数据
                }
                else
                {
                    Response.Write("<script>alert('非法登录!即将返回登录页面!');location.href = 'Login.aspx';</script>");
                }
            }
        }
        /// <summary>
        /// 显示数据
        /// </summary>
        public void ShowData()
        {
            if (index == 0)//禁用上一页按键
                btn_up.Enabled = false;
            else
                btn_up.Enabled = true;
            if (NoveBLL.NoveCount() <= (index + 1) * 8)//禁用下一页按键
                btn_down.Enabled = false;
            else
                btn_down.Enabled = true;
            span_index.InnerHtml = "第" + (index + 1) + "页";
            //获取所有小说并绑定到前端页面
            rpt.DataSource = NoveBLL.SelectNove(ddl_time.SelectedValue,ddl_type.SelectedValue,text_Novename.Text,index);
            rpt.DataBind();
        }
        /// <summary>
        /// 当下拉框或文本框的值发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnChanged(object sender, EventArgs e)
        {
            ShowData();
        }
        /// <summary>
        /// 删除超链接点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void a_del_Command(object sender, CommandEventArgs e)
        {
            //获取对应点击的小说ID
            int id = int.Parse(e.CommandArgument.ToString());
            string name = e.CommandName;
            if (NoveBLL.NoveDel(id))//执行删除命令并获取返回值
            {
                //删除小说封面和小说路径
                if (File.Exists(Server.MapPath("/image/" + name + ".jpg")))
                    File.Delete(Server.MapPath("/image/" + name + ".jpg"));
                else if(File.Exists(Server.MapPath("/image/" + name + ".png")))
                    File.Delete(Server.MapPath("/image/" + name + ".png"));
                if (Directory.Exists(Server.MapPath("/text/" + name)))
                    Directory.Delete(Server.MapPath("/text/" + name));
                Response.Write("<script>alert('删除成功！')</script>)");
            }
            ShowData();
        }
        /// <summary>
        /// 上一页点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_up_Click(object sender, EventArgs e)
        {
            index -= 1;
            ShowData();
        }
        /// <summary>
        /// 下一页点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_down_Click(object sender, EventArgs e)
        {
            index += 1;
            ShowData();
        }
        /// <summary>
        /// 新增小说点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_add_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddNove.aspx");
        }
    }
}